#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "backlight.h"
#include "lcd.h"
#include "eeprom.h"
#include "body.h"


uint16_t wBacklightTime = 0;
uint8_t wBacklight = 0;
uint8_t target = 0;


void BACKLIGHT_Set( uint16_t wTime )
{
	wBacklight = Eeprom.backlightLevel;
	wBacklightTime = wTime * 50;	//assumes 20ms rate
	LCD_SetBacklight( wBacklight );
}


void BACKLIGHT_Kick( void )
{
	BACKLIGHT_Set( Eeprom.backlightTime );
}


void BACKLIGHT_Tick25ms( void )
{
	if( BODY_LIGHTS ) {
		target = Eeprom.backlightLevelLightsOn;
	}
	else {
		target = Eeprom.backlightLevelLightsOff;
	}
	
	if( wBacklightTime ) {
		wBacklightTime--;
	}
	else {
		if( wBacklight < target ) {
			wBacklight++;
		}
		else if( wBacklight > target ) {
			wBacklight--;
		}
		LCD_SetBacklight( wBacklight );
	}
}
